/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelBiseccion.java
 *
 * Created on 11-oct-2010, 14:12:47
 */
package interfaz.FuncionPanels;

//import java.awt.FlowLayout;
//import javax.print.DocFlavor.STRING;
//import javax.management.monitor.GaugeMonitor;
import interfaz.NuMethodApp;
import interfaz.PanelMain;
import java.awt.Color;
import java.io.IOException;
import javax.swing.JFrame;
//import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import metodos.inpol.SplinesCubicos;
import metodos.sistec.GaussianaPivParcial;
import metodos.sistec.LibreriaBase;

/**
 *
 * @author The NuMethod Team
 */
public class PanelSplinesCubicos extends javax.swing.JPanel {

    private static DefaultTableModel ma;
    private static NuMethodApp parent;

    public PanelSplinesCubicos(NuMethodApp parent) {
        ma = new DefaultTableModel();
        initComponents();
        initComponents2();
        this.parent = parent;
    }

     public void setlinksColor (Color c){
        jXHyperlink1.setClickedColor(c);
        jXHyperlink1.setUnclickedColor(c);

    }

    // <editor-fold defaultstate="collapsed" desc="Helpful getters">
    public static DefaultTableModel getMa() {
        return ma;
    }

    // </editor-fold>
    private void initComponents2() {
        //this.jTable1.setVisible(false);
        TablePuntos.setShowGrid(true);
        TablePuntos.setOpaque(true);
        TablePuntos.setAutoStartEditOnKeyStroke(true);
        TablePuntos.setRolloverEnabled(true);
        TablePuntos.setSurrendersFocusOnKeystroke(true);
        TablePuntos.setTerminateEditOnFocusLost(true);
        TablePuntos.setFillsViewportHeight(true);

        TablePuntos.setEditable(false);

        TablePuntos.setVisibleColumnCount(0);

        rebuildTableModels(3);

        LabelStatus.setText("");


    }

    public void updateTableModels() {
        //COnfigurar los modelos de tabla para las 3 tablas disponibles.
        DefaultTableModel m = PanelInterpolacion.getMa();
        TablePuntos.setModel(m);

        m.fireTableDataChanged();
    }

    /**
     * Metodo que reconstruye de forma dinamica los modelos de tabla
     * luego de cada entrada de datos
     * @param a el numero de ecuaciones del sistema
     */
    private void rebuildTableModels(Integer a) {
        //Constuir el modelo para la tabla A
        Double[][] test = new Double[2][a];
        String[] names = new String[a];

        for (int i = 0; i < a; i++) {
            String aTest = "Punto (x" + (i) + "," + "y" + (i) + ")";
            names[i] = aTest;
        }

        ma = new DefaultTableModel(test, names);
        TablePuntos.setModel(ma);

        ma.fireTableDataChanged();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        TablePuntos = new org.jdesktop.swingx.JXTable();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        TableResult = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jXHyperlink1 = new org.jdesktop.swingx.JXHyperlink();
        LabelStatus = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        TableResult2 = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setToolTipText("Metodo de los Splines Cubicos");

        TablePuntos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(TablePuntos);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/help.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText("Matriz A");

        jLabel1.setText("Metodo de los Splines Cubicos");

        TableResult.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(TableResult);

        jButton1.setText("Ejecutar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Cuidado: Los puntos solo pueden ser modificados desde el panel de entrada");

        jXHyperlink1.setText("Ir al panel de entrada");
        jXHyperlink1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink1ActionPerformed(evt);
            }
        });

        LabelStatus.setText("jLabel3");

        jScrollPane4.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        TableResult2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TableResult2.setFillsViewportHeight(true);
        jScrollPane4.setViewportView(TableResult2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 616, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 546, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(262, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(521, Short.MAX_VALUE)
                .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(LabelStatus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 500, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(19, 19, 19))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 616, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(28, 28, 28)
                        .addComponent(jLabel4))
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(LabelStatus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(86, 86, 86))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
            NuMethodApp.helpLauncher("SplinesCubicos.html");
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * Metodo que extrae de los textfield`s los valores de los parametros
     * para los metodos iterativos.
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        double[] x = new double[TablePuntos.getModel().getColumnCount()];
        double[] fx = new double[TablePuntos.getModel().getColumnCount()];

        if (x.length >= 3) {
            for (int i = 0; i < x.length; i++) {
                x[i] = Double.parseDouble((String) TablePuntos.getModel().getValueAt(0, i));
                fx[i] = Double.parseDouble((String) TablePuntos.getModel().getValueAt(1, i));
            }

            double[][] result = SplinesCubicos.solucionar(x, fx);
            double[] ind = SplinesCubicos.ind;

            double[][]baseModel = LibreriaBase.extenderMatriz(result, ind);

            Double[][] newModel = new Double[baseModel.length][baseModel[0].length];
            int lim1 = baseModel.length;
            int lim2 = baseModel[0].length;
            for (int i = 0; i < lim1; i++) {
                for (int j = 0; j < lim2; j++) {
                    newModel[i][j] = new Double(baseModel[i][j]);
                }
            }

            String[] names = new String[newModel[0].length];
            for (int i = 0; i < (result.length / 4); i++) {
                String help1 = "A" + String.valueOf(i + 1);
                String help2 = "B" + String.valueOf(i + 1);
                String help3 = "C" + String.valueOf(i + 1);
                String help4 = "D" + String.valueOf(i + 1);
                int j = i * 4;
                names[j] = help1;
                names[j + 1] = help2;
                names[j + 2] = help3;
                names[j + 3] = help4;
            }
            names[names.length - 1] = "ind";

            DefaultTableModel dt = new DefaultTableModel(newModel, names);
            TableResult.setModel(dt);
            dt.fireTableDataChanged();
            /////////

            double[][][]faces = GaussianaPivParcial.solucionar(result, ind);
            double[] result2 = GaussianaPivParcial.obtenerResultado(faces[faces.length - 1]);


            int r = x.length - 1;
            String[][] test = new String[r][1];
            String[] names2 = {"Ecuaciones"};
            for (int i = 0; i < r; i++) {
                int j = i * 4;
                StringBuilder s = new StringBuilder();
                s.append(result2[j]).append("x^3 + ").append(result2[j + 1]).append("x^2 + ").append(result2[j + 2]).append("x + ").append(result2[j+3]);
                s.append(" -> cuando (").append(x[i]).append(" < x <").append(x[i + 1]).append(")");
                test[i][0] = s.toString();
            }
            
            DefaultTableModel dt2 = new DefaultTableModel(test, names2);
            TableResult2.setModel(dt2);
            dt2.fireTableDataChanged();

        } else {
            LabelStatus.setText("Este metodo necesita mas de 3 puntos para funcionar, por favor cambia el numero de puntos");

        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jXHyperlink1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink1ActionPerformed
        parent.getPanelMain().cardChanger(PanelMain.INTERPOLACION);
    }//GEN-LAST:event_jXHyperlink1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LabelStatus;
    private org.jdesktop.swingx.JXTable TablePuntos;
    private javax.swing.JTable TableResult;
    private javax.swing.JTable TableResult2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink1;
    // End of variables declaration//GEN-END:variables
}
